package dmwr.context.gae;

import javax.servlet.http.HttpSession;

import com.google.appengine.api.utils.SystemProperty;

import dmwr.bean.User;
import dmwr.context.Context;
import dmwr.context.Environment;
import dmwr.util.Operation;
import dmwr.web.Http;

public class ContextProviderGae implements Operation<Context> {

	@Override
	public Context execute() throws Exception {
		return new Context() {
			@Override
			public Environment getEnvironment() {
				switch (SystemProperty.environment.value()) {
				case Development:
					return Environment.DEVELOPMENT;
				case Production:
					return Environment.PRODUCTION;
				default:
					return null;
				}
			}

			@Override
			public User getUser() {
				HttpSession session = Http.request().getSession(false);
				return session != null ? (User) session.getAttribute("user")
						: null;
			}
		};
	}
}
